<?php
namespace app\admin\model;
use think\Model;
use think\Db;

class CmppServerRegCenter extends Model
{
    // 设置当前模型对应的完整数据表名称
    protected $table = 'bus_cmpp_server_reg_center';

    /**
     * 根据ID查询
     */
    public function getCmppData($id)
    {
        $data = $this::where('id', '=', $id)->find();
        if ($data) {
            return $data->toArray();
        } else {
            return $data;
        }
    }

    /**
     * [getServiceAlls 分页获取服务器列表]
     * @author   .guo
     * @DataTime 2019-05-31
     * @param    [type]     $request [description]
     * @return   [type]              [description]
     */
    public function getServiceAlls($request)
    {
        $data = [];

        if(session('user_type')=='admin') {
        
            $data = $this
                ->field('*')
                ->paginate($request['limit'], false, $request)
                ->each(function($item, $key) {

                switch ($item->onoff) {

                    case 1:
                        $item->onoff_ = '<span class="layui-badge layui-bg-green status-qy">启用</span>';
                        break;

                    default:
                        $item->onoff_ = '<span class="layui-badge status-jy">禁用</span>';
                        break;
                }
            });
        }

        return $data;
    }

    /**
     * 添加
     */
    public function addDataOne($data)
    {
        $data['createtime'] = date('Y-m_d H:i:s');
        $result = $this->save($data);

        if ($result !== false)
        {
            trace('用户'.session('username').'添加CMPP服务中心: ' . json_encode($data,JSON_UNESCAPED_UNICODE), 'info');
            return ['code' => 1, 'msg' => '添加成功'];
        }
        else
        {
            return ['code' => 0, 'msg' => '添加失败'];
        }
    }

    /**
     * 编辑
     */
    public function updateDataOne($data)
    {
        $result = $this->save($data, ['id' => $data['id']]);

        if ($result !== false)
        {
            trace('用户'.session('username').'编辑CMPP服务器:'.json_encode($data,JSON_UNESCAPED_UNICODE),'info');
            return ['code' => 1, 'msg' => '编辑成功'];
        }
        else
        {
            return ['code' => 0, 'msg' => '编辑失败'];
        }
    }
}